package com.j283_5.wisdom_park.controller.investment_management.contract;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.j283_5.wisdom_park.common.result.ResultData;
import com.j283_5.wisdom_park.mapper.ContractTableMapper;
import com.j283_5.wisdom_park.pojo.CollectionRecord;
import com.j283_5.wisdom_park.pojo.ContractTable;
import com.j283_5.wisdom_park.pojo.Visitors;
import com.j283_5.wisdom_park.service.ContractTableService;
import com.j283_5.wisdom_park.vo.contract_management.CollectionRecordVo;
import com.j283_5.wisdom_park.vo.contract_management.ContractAddVo;
import com.j283_5.wisdom_park.vo.contract_management.ContractTableVo;
import com.j283_5.wisdom_park.vo.contract_management.RoomVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.io.FilenameUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.websocket.server.PathParam;
import java.io.File;
import java.io.IOException;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * <p>
 * 合同表 前端控制器
 * </p>
 *
 * @author 冯弋舟
 * @since 2022-04-28
 */
@RestController
@RequestMapping("/contract-table")
@Api(value = "招商合同管理",tags = "招商合同管理",produces="冯弋舟")
public class ContractTableController {

    @Resource
    private ContractTableMapper mapper;

    @Resource
    private ContractTableService service;
    /**
     * 多条件查询合同 (ok)
     * @return
     */
    @RequestMapping(value = "/findContractTable" , method = RequestMethod.GET)
    @ApiOperation(value = "查询所有合同信息",notes = "查询所有合同信息（分页）",produces="冯弋舟", response = ResultData.class)
    public ResultData findContractTable(@Param("page") Page<Visitors> page, ContractTableVo contractTableVo){
        Map<String, Object> contractTable = service.findContractTable(contractTableVo, (int) page.getCurrent(), (int) page.getSize());
        return ResultData.ok().data(contractTable);
    }

    /**
     * 根据id查询合同详情
     * @return
     */
    @RequestMapping(value = "/findContractTableById" , method = RequestMethod.GET)
    @ApiOperation(value = "根据id查询合同",notes = "根据id查询合同",produces="冯弋舟", response = ResultData.class)
    public ResultData findContractTableById(Integer contract_id){
       return   service.findContractTableInfoById(contract_id);
    }

    /**
     * 查询租凭房间信息
     * @param contract_id
     * @return
     */
    @RequestMapping(value = "/findRoom",method = RequestMethod.GET)
    @ApiOperation(value = "查询租凭房间信息",notes = "查询租凭房间信息",produces="冯弋舟", response = ResultData.class)
    public ResultData findRoom(Integer contract_id){
        Map<String, Object> map = service.findRoomById(contract_id);
        return ResultData.ok().data(map);
    }

    /**
     * 查询收款信息
     * @param contract_id
     * @return
     */
    @RequestMapping(value = "/findCollection_record",method = RequestMethod.GET)
    @ApiOperation(value = "查询收款信息",notes = "查询收款信息",produces="冯弋舟", response = ResultData.class)
    public ResultData findCollection_record(Integer contract_id){
        Map<String, Object> map = service.findCollectionById(contract_id);
        return ResultData.ok().data(map);
    }

    /**
     * 删除合同
     * @param contract_id
     * @return
     */
    @RequestMapping(value = "/delete_ContractTable",method = RequestMethod.POST)
    @ApiOperation(value = "删除合同",notes = "删除合同）",produces="冯弋舟", response = ResultData.class)
    public ResultData delete_ContractTable(@RequestBody List<Integer> contract_id){

        return service.deleteContractTable(contract_id);
    }

    /**
     * 新增合同
     * @param contractTableVo
     * @return
     */
    @RequestMapping(value = "/insert_ContractTable",method = RequestMethod.POST)
    @ApiOperation(value = "新增合同",notes = "新增合同",produces="冯弋舟", response = ResultData.class)
    public ResultData insert_ContractTable(ContractTableVo contractTableVo, CollectionRecordVo collectionRecordVo){

        Integer Per_id = service.findContractAddPer_IdVo(contractTableVo);
        Integer Room_id = mapper.findContractAddRoom_idVo(contractTableVo);
        Integer Ten_id = mapper.findContractAddTen_IdVo(contractTableVo);
        ContractAddVo contractAddVo=new ContractAddVo();
        contractAddVo.setPer_id(Per_id);
        contractAddVo.setRoom_id(Room_id);
        contractAddVo.setTen_id(Ten_id);
        int i = service.insertContract(contractTableVo, contractAddVo);
        int j = service.insertRoom(contractTableVo, contractAddVo);
        int k = service.insertCollection_record(collectionRecordVo, contractAddVo);
        if (i>0&&j>0&&k>0){
            return ResultData.ok();
        }else {
            return  ResultData.error();
        }

    }

    /**
     * 签订合同
     * @param contract_id
     * @return
     */
    @RequestMapping(value = "/DoContractTable",method = RequestMethod.POST)
    @ApiOperation(value = "签订合同",notes = "签订合同",produces="冯弋舟", response = ResultData.class)
    public ResultData DoContractTable(@RequestBody List<Integer> contract_id){

        return service.deleteContractTable(contract_id);
    }
}

